package cn.itsource.basic.controller;
//文件上传的controlle

import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.FastDfsApiOpr;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/common")
public class FastDfsController {
    //文件上传
    @PostMapping("/fastdfsUpload")
    public AjaxResult upload(@RequestPart(required = true,value = "file") MultipartFile file ){
        //System.out.println(file);
        try {
            byte[] bytes = file.getBytes();
            //upload(byte[] file,String extName) extName:文件的后缀名字
            //获取文件的原始名字
            String originalFilename = file.getOriginalFilename();
            //获取后缀
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
            System.out.println(extName);
            String upload = FastDfsApiOpr.upload(bytes, extName);
            //上传完成以后返回的路径
            System.out.println(upload);
            return  AjaxResult.me().setResultObj(upload);
        } catch (IOException e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }


    }



}
